/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package relationships;

import pedigreereconstruction.Individual;
import pedigreereconstruction.PedigreeReconstruction;

/**
 *
 * @author arthiramachandran
 */
public abstract class Relationship {

    public final Individual individual1;
    public final Individual individual2;
    public final double totalSharedIBD;
    public final int numberOfSharedSegments;
    public final double averageSharedIBD;
    public final RelationshipTypes relationshipType;

    public Relationship(Individual i1, Individual i2, double total, int num,
            double avg, int up, int down, int founders) {
        individual1 = i1;
        individual2 = i2;
        totalSharedIBD = total;
        numberOfSharedSegments = num;
        averageSharedIBD = avg;
        relationshipType = new RelationshipTypes(up, down, founders);
    }

    @Override
    public String toString() {
        return "<" + individual1 + "-" + individual2 + ">";
        //return "<" + individual1 + "," + individual2 + "(" + totalSharedIBD + "," + numberOfSharedSegments + "," + averageSharedIBD + ")>";
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash =
                89 * hash + (this.individual1 != null ? this.individual1.hashCode() : 0);
        hash =
                89 * hash + (this.individual2 != null ? this.individual2.hashCode() : 0);
        hash =
                89 * hash + (this.relationshipType != null ? this.relationshipType.hashCode() : 0);
        return hash;
    }

    /*@Override
    public int hashCode() {
        return (int) (31 * (individual1.hashCode() + individual2.hashCode() + totalSharedIBD + numberOfSharedSegments + averageSharedIBD));
    }
     * 
     */

    @Override
    public boolean equals(Object obj) {
        System.out.println("--");

        if (obj instanceof Relationship) {
            Relationship r = (Relationship) obj;
            /*
            if (r.individual1.id.contains("K1289") || r.individual2.id.contains(
            "K1289")) {
            if (this.individual1.id.contains("K5762") || this.individual2.id.
            contains("K5762")) {
            System.out.println("!!!" + r + "\t" + this);
            }
            }
             * 
             */
            if (!r.relationshipType.equals(this.relationshipType)) {
                return false;
            }
            /*if (r.individual1.isMissing()) {
            return (r.individual2.equals(individual2)
            || r.individual2.equals(individual1));
            } else if (r.individual2.isMissing()) {
            return (r.individual1.equals(individual2)
            || r.individual1.equals(individual1));
            } else if (this.individual1.isMissing()) {
            return (r.individual1.equals(individual2)
            || r.individual2.equals(individual2));
            } else if (this.individual2.isMissing()) {
            return (r.individual1.equals(individual1)
            || r.individual2.equals(individual1));
            } else 
             */
            {
                return ((r.individual1.equals(individual1)
                        && r.individual2.equals(individual2))
                        || (r.individual1.equals(individual2)
                        && r.individual2.equals(individual1)));
            }

        }
        return false;

    }

    public Individual getOtherIndividual(Individual i) {
        if (this.individual1.equals(i)) {
            return this.individual2;
        } else {
            return this.individual1;
        }
    }

    public boolean equalsIgnoreMissingParent(Object obj) {
        //System.out.println("!!!");
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ProbabilisticRelationship other = (ProbabilisticRelationship) obj;
        if (this.relationshipType != other.relationshipType) {

            return false;
        }

        // specific case where the parent is missing in one of the relationships 
        // - maybe one relationship can be used to fill in the blanks
        if (this.relationshipType.equals(PedigreeReconstruction.parent_child)) {
            if (this.individual1.isMissing() || other.individual1.isMissing()) {
                return this.individual2.equals(other.individual2);
            }
        }

        return (this.individual1.equals(other.individual1)
                && this.individual2.equals(other.individual2));
    }

    /*
    public boolean equals(Object obj) {
    if (obj instanceof Relationship) {
    Relationship r = (Relationship) obj;
    return (r.individual1.equals(individual1) 
    && r.individual2.equals(individual2) 
    && r.totalSharedIBD == totalSharedIBD
    && r.numberOfSharedSegments == numberOfSharedSegments
    && r.averageSharedIBD == averageSharedIBD
    );
    
    }
    return false;
    
    }
     * 
     */
    //void setIndividuals(Individual i1, Individual i2);
    //public void setSharing(Integer totalSharedIBD, Integer numberOfSharedSegments);
    //public Boolean isIndividualInRelationship(Individual i);
    public boolean containsMissing() {
        return (this.individual1.isMissing() || this.individual2.isMissing());
    }

    public boolean containsIndividual(Individual i) {
        return (this.individual1.missingEquals(i) || this.individual2.missingEquals(i));
    }
}
